﻿#pragma warning disable CS8618 
using System.Text;
using Microsoft.Extensions.DependencyInjection;

namespace AspNetCore.Host.Hosting
{
    public class HttpContext
    {
        public HttpRequest Request { get; }
        public HttpResponse Response { get; }
        public IFeatureCollection Features { get; }
        public IServiceProvider RequestServices { get; }
        
        public HttpContext(HttpRequest request, IServiceProvider requestServices)
        {
            Request = request;
            Response = new HttpResponse();
            RequestServices = requestServices;
            var features = requestServices.GetRequiredService<IFeatureCollection>();
            Features = new FeatureCollection(features);
        }
    }

    public class HttpRequest
    {
        public string Url { get; set; }
        public string Method { get; set; }
        public string From { get; set; }
    }
    public class HttpResponse
    {
        public MemoryStream _stream = new MemoryStream();
        
        public void Write(byte[] bytes)
        {
            _stream.Write(bytes, 0, bytes.Length);
        }
        public async Task WriteAsync(byte[] bytes)
        {
            await _stream.WriteAsync(bytes, 0, bytes.Length);
        }

        public void Write(string message)
        {
            Write(Encoding.UTF8.GetBytes(message));
        }

        public async Task WriteAsync(string message)
        {
            await WriteAsync(Encoding.UTF8.GetBytes(message));
        }

        public byte[] GetBuffer()
        {
            return _stream.GetBuffer();
        }
    }
}
